package com.wangbo.hutool;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wangbo.restTemplate.camviNew.TestCamviNew;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RestTemplate;
import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.File;

@Slf4j
public class TestImgUtil {

    public static void main(String[] args) {
        RestTemplate restTemplate = new RestTemplateBuilder().errorHandler(new DefaultResponseErrorHandler(){
            /**
             * 设置RestTemplate自带的异常处理器不做异常处理
             **/
            @Override
            public void handleError(ClientHttpResponse response) {
            }
        }).build();

        File file = new File("D:\\temp\\11971.jpg");

        // 人脸侦测 & 人脸截取
//        test01();

        // 水平翻转
        test02(restTemplate, file);
    }

    /**
     * 人脸侦测 & 人脸截取
     */
    public static void test01(File file, RestTemplate restTemplate) {

        byte[] imgBytes = FileUtil.readBytes(file);

        JSONObject jsonObject = TestCamviNew.test06(restTemplate, imgBytes, file.getName());
        log.info("人脸侦探结果：{}", jsonObject.toString());

        String status = jsonObject.getString("status");
        if ("success".equals(status)) {
            Integer faceCountInput = jsonObject.getInteger("face_count_input");
            if (faceCountInput > 1) {
                System.out.println("侦测到多张人脸");
            } else {
                JSONArray faces = jsonObject.getJSONArray("faces");

                JSONObject face = faces.getJSONObject(0);
                Boolean detectMask = face.getBoolean("detect_mask");
                Float sharpness = face.getFloat("sharpness");
                Float pitch = face.getFloat("pitch");
                Float roll = face.getFloat("roll");
                Float yaw = face.getFloat("yaw");
                Float eye1_open = face.getFloat("eye1_open");
                Float eye2_open = face.getFloat("eye2_open");
                int width = face.getIntValue("width") + 100;
                int height = face.getIntValue("height") + 100;

                if (detectMask) {
                    System.out.println("侦测到口罩");
                }
                if (sharpness < 3) {
                    System.out.println("清晰度太低");
                }
                if (pitch > 15 || pitch < -15 || roll > 15 || roll < -15 || yaw > 15 || yaw < -15) {
                    System.out.println("人脸朝向不符合要求");
                }
                if (eye1_open < 0.3 || eye2_open < 0.3) {
                    System.out.println("眼睛未张开");
                }

                int x = face.getIntValue("topleftx") - 50;
                int y = face.getIntValue("toplefty") - 50;
                Image image = ImgUtil.read(file);
                System.out.println("图片width=" + image.getWidth(null));
                Rectangle rectangle = new Rectangle(x, y, width, height);
                image = ImgUtil.cut(image, rectangle);

                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ImgUtil.scale(image, baos, 600 / Math.min(width, height));
                byte[] bytes = baos.toByteArray();
                FileUtil.writeBytes(bytes, "D:\\temp\\aaa.jpg");
            }
        } else {
            System.out.println("未侦测到人脸");
        }
    }

    /**
     * 水平翻转
     */
    public static void test02(RestTemplate restTemplate, File file) {
        Image image = ImgUtil.read(file);
        image = ImgUtil.flip(image);
        ImgUtil.write(image, new File("D:\\temp\\" + RandomUtil.randomString(4) + ".jpg"));
    }
}
